<?php namespace App\Http\Controllers\Merchant;

use App\Category;
use App\Http\Requests;
use App\Http\Controllers\Controller;

use App\Job;
use App\User;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Cache;

class ProductController extends Controller
{
    public function getIndex(Request $request)
    {
        $appends = $request->all();
        $users = User::orderBy('name')->lists('nick','id');
        $list = Job::orderBy('pubtime','DESC')->where('userid',\Auth::user()->id);
        if (isset($appends['name']) && "" != $appends['name']) {
            $list = $list->where('title', 'like', '%' . $appends['name'] . '%');
        }
        if (isset($appends['parentcategory']) && "" != $appends['parentcategory']) {
            $list = $list->where('parentcategory',$appends['parentcategory']);
        }
        $list = $list->paginate(config('site.admin.pageSize'));
        if (Cache::has('parent') && 1 == 2) {
            $parent = Cache::get('parent');
        } else {
            $parent = Category::where('rank', '1')->lists('name', 'categoryid');
            Cache::forever('parent', $parent);
        }
        return view('merchant.products.index', [
            'list' => $list,
            'parent' => $parent,
            'users' => $users,
            'appends' => $appends,
        ]);
    }

    public function getNewj($id = 0)
    {
        $model = Job::findOrNew($id);
        if(0 != $id && \Auth::user()->id != $model->userid){
            abort(404);
        }
        if (Cache::has('parent') && 1 == 2) {
            $parent = Cache::get('parent');
        } else {
            $parent = Category::where('rank', '1')->lists('name', 'categoryid');
            Cache::forever('parent', $parent);
        }
        $categorys = Category::where('rank', '2')->get();
        return view('merchant.products.newj', [
            'model' => $model,
            'parents' => $parent,
            'categorys' => $categorys,
        ]);
    }

    public function postNewj(Request $request, $id = 0)
    {
        $model = Job::findOrNew($id);
        if(0 != $id && \Auth::user()->id != $model->userid){
            abort(404);
        }
        $data = $request->all();
        $rules = [
            "name" => "required",
            "imgurl" => "required",
            "parentcategory" => "required",
            "categoryid" => "required",
            "endtime" => "required",
            "starttime" => "required",
            "addr" => "required",
            "describe" => "required",
        ];
        $attributes = [
            "name" => "名称",
            "imgurl" => "显示图片",
            "parentcategory" => "分类",
            "categoryid" => "分类",
            "starttime" => "开工时间",
            "endtime" => "结束时间",
            "addr" => "地址",
            "describe" => "描述",
        ];
        $validator = \Validator::make($data, $rules, ['required' => ':attribute不能为空'], $attributes);
        if ($validator->fails()) {
            $ret = ['state' => 'fail'];
            $message = $validator->messages();
            $ret['message'] = $message;
            foreach ($attributes as $k => $attr) {
                if ($message->has($k)) {
                    $ret['message'] = $message->first($k);
                    break;
                }
            }
        } else {
            if (Cache::has('parent') && 1 == 2) {
                $parent = Cache::get('parent');
            } else {
                $parent = Category::where('rank', '1')->lists('name', 'categoryid');
                Cache::forever('parent', $parent);
            }
            $categorys = Category::find($data['categoryid']);
            $data['parentkind'] = $parent[$data['parentcategory']];
            $data['kind'] = $categorys->name;
            $data['pubtime'] = isset($model->pubtime) ? $model->pubtime : date("Y-m-d H:i:s");
            if ("" == $data['discount']) {
                unset($data['discountunit']);
            }
            if (!$model->jobid) {
                $model->userid = \Auth::user()->id;
            }
            $model->fill($data);
            $model->save();//写入数据库
            if ($model->jobid) {
                $ret = ['state' => 'success', 'message' => '操作成功'];
            } else{
                $ret = ['state' => 'fail'];
                $ret['message'] = '保存出错';
            }
        }
        return $ret;
    }

    public function getDelj($id = 0){
        $model = Job::findOrNew($id);
        if(0 != $id && \Auth::user()->id != $model->userid){
            abort(404);
        }
        return view('merchant.products.delj', [
            'model' => $model,
        ]);
    }

    public function postDelj(Request $request ,$id = 0){
        $model = Job::findOrNew($id);
        if(0 != $id && \Auth::user()->id != $model->userid){
            abort(404);
        }
        $data = $request->all();
        $model->reason = $data['reason'];
        $model->canceltime = date("Y-m-d H:i:s");
        $model->state = '已取消';
        $model->save();
        if ($model->jobid) {
            $ret = ['state' => 'success', 'message' => '操作成功'];
        } else{
            $ret = ['state' => 'fail'];
            $ret['message'] = '删除失败';
        }
        return \Response::json($ret);
    }
}